<?php

namespace App\Http\Services\Admin;


class LocalBladeProvider
{

    public function setQuery($url = '', $query = [], $params = [])
    {
        if(isset($query['page'])){
            unset($query['page']);
        }
        $f_url = '';
        if (!count($params)) {
            $f_url = count($query) ? $url . '?' . http_build_query($query) : $url;
        } else if (!count($query)) {
            $f_url = count($params) ? $url . '?' . http_build_query($params) : $url;
        } else {
            foreach ($params as $key => $item) {
                if (isset($query[$key])) {
                    $value_arr = explode(',', $query[$key]);
                    if(in_array($item,$value_arr)){
                        unset($value_arr[array_search($item,$value_arr)]);
                    }
                    else {
                        array_push($value_arr, $item);
                    }

                    if(count($value_arr)) {
                        $query[$key] = implode(',', $value_arr);
                    }
                    else{
                        unset($query[$key]);
                    }
                } else {
                    $query[$key] = $item;
                }
            }
            $f_url = $url . '?' . http_build_query($query);
        }
        return $f_url;
    }

    public function isChecked($query = [],$params = []){
        $is_checked = false;
        if(!count($params)){
            return $is_checked;
        }

        foreach ($params as $key => $item){
            if(isset($query[$key])) {
                $value_arr = explode(',', $query[$key]);
                if (count($value_arr) && in_array($item, $value_arr)) {
                    $is_checked = true;
                }
            }
        }
        return $is_checked;
    }
}
